package y.ryyan.msgswitch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

@Controller
public class KinkooController {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;//1

    @MessageMapping("/welcome")
    @SendTo("/topic/getResponse")
    public KinkooResponse say(KinkooMsg msg) throws Exception{
        Thread.sleep(3000);
        return new KinkooResponse("welcome,"+msg.getName()+"!");
    }

    @MessageMapping("/chat")
    public void handleChat(Principal principal, String msg) {//2
        if (principal.getName().equals("yqy")) {//3
            messagingTemplate.convertAndSendToUser("ryan","/queue/notifications",principal.getName() + "-send:"+msg);//4
        }else{
            messagingTemplate.convertAndSendToUser("yqy","/queue/notifications",principal.getName()+"-send:"+msg);
        }
    }
}
